<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      table {
        width: 500px;
        text-align: center;
        border-collapse: collapse;
      }
      th,
      td {
        border: 1px solid purple;
      }
    </style>
  </head>

  <body>
    <input type="text" placeholder="请输入订单状态" class="orderStatus" />
    <button>筛选订单</button><br /><br />
    <table>
      <thead>
        <tr>
          <th>订单编号</th>
          <th>姓名</th>
          <th>地址</th>
          <th>状态</th>
          <th>其他</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>

    <script>
      const orders = [

        {
          id: 1,
          name: 'John Smith',
          address: '123 Main St',
          status: 'created',
          log: [
            {
              status: 'created',
              time: '2023-02-16 12:30:00',
            },
          ],
        },
        {
          id: 2,
          name: 'Jane Doe',
          address: '456 Second St',
          status: 'packed',
          log: [
            {
              status: 'packed',
              time: '2023-02-16 12:30:00',
            },
            {
              status: 'created',
              time: '2023-02-16 10:00:00',
            },
          ],
        },
        {
          id: 3,
          name: 'Bob Johnson',
          address: '789 Third St',
          status: 'shipped',
          log: [
            {
              status: 'created',
              time: '2023-02-15 10:00:00',
            },
            {
              status: 'packed',
              time: '2023-02-15 12:00:00',
            },
            {
              status: 'shipped',
              time: '2023-02-16 9:00:00',
            },
          ],
        },
        {
          id: 4,
          name: 'zs',
          address: '789 Third St',
          status: 'created',
          log: [
            {
              status: 'created',
              time: '2023-04-15 10:00:00',
            },
          ],
        },
      ]
    


      const tbody = document.querySelector('tbody')
      const btn = document.querySelector('button')
      const input = document.querySelector('.orderStatus');

      // 点击筛选
      btn.addEventListener('click', function () {
        // console.log(1);
        if(!input.value){
          alert('不能为空')
          render(orders)
        }else {
          const filter  = orders.filter((item)=>{
            return item.status === input.value
          })
          render(filter)
        }
      })

      // 渲染函数
      function render(data){
       const html =  data.map((item)=>{
          return `
           <tr>
                <td>${item.id}</td>
                <td>${item.name}</td>
                <td>${item.address}</td>
                <td>${item.status}</td>
                <td><a href="javascript:;" data-id=${item.id}>查看物流详情</a></td>
            </tr>
          `
         })
         tbody.innerHTML = html.join('') || []
      }
      render(orders)
      // 点击查看物流信息
      tbody.addEventListener('click', function (e) {
        if(e.target.tagName === 'A'){
          const id = +e.target.dataset.id

          // find 数组方法，返回条件符合那一项
          const res = orders.find((item)=>{return item.id === id})
          // console.log(res);
          const {log} = res
          // 排序
          const sortLog = log.sort((a,b)=>{
            const timeA = +new Date(a.time)
            const timeB = +new Date(b.time)
            return timeA - timeB
          })

          // console.log(log);
          // 弹出物流信息，遍历点击那一项的log
          alert(`物流信息：
           ${sortLog.map((item)=>{return `
            [${item.time}]-[${item.status}]
            `}).join('')}
           `)
        }
      })
    </script>
  </body>
</html>
